package com.tecnap.app.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * DatabaseHelper
 * This class is used to create and upgrade the database,
 * and also contain all database structure (Tables and Columns)
 * @author joe
 */
public class DatabaseHelper extends SQLiteOpenHelper {
	
	// Database name
	private static final String DATABASE_NAME = "tecnap.db";
	
	// Database table column ID
	public static final String COLUMN_ID = "_id";
	
	// Database tables
	public static final String TABLE_CONFIG = "config";
	public static final String TABLE_LAMP = "lamp";
	
	// SQL Statement to create database
	private static final String[] DATABASE_SQL = {
		// 1. CREATE TABLE CONFIG
		"CREATE TABLE " + TABLE_CONFIG + 
		" (" +
		" key CHARACTER(20) PRIMARY KEY, " +
		" value CHARACTER(50) " +
		")",
		// 2. CREATE TABLE LAMP
		"CREATE TABLE " + TABLE_LAMP +
		" (" +
		" lamp" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
		" number CHARACTER(30) UNIQUE, " +
		" type CHARACTER(30), " +
		" brand CHARACTER(30), " +
		" power CHARACTER(30), " +
		" model CHARACTER(30), " +
		" comment TEXT, " +
		" X REAL, " +
		" Y REAL " +
		")",
		// 3. POPULATE CONFIG TABLE
		"INSERT INTO config VALUES ('google_api_key', '0mKCM9beB8Rd971GAilRC7-VeqBb_ucyf8OC_Rg')",
	}; 
	
	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// Create the database
		for (int i = 0; i < DATABASE_SQL.length; i++) {
			db.execSQL(DATABASE_SQL[i]);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO
	}

}
